<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>常用模块 | Teng&#39;s blog</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/logo/favicon.ico">
    <meta name="description" content="">
    <meta name="keywords" content="大数据,java,h5,前端,人工智能,深度学习,go">
    <meta name="theme-color" content="#3eaf7c">
    
    <link rel="preload" href="/assets/css/0.styles.c17b3ac5.css" as="style"><link rel="preload" href="/assets/js/app.ab6a7ec7.js" as="script"><link rel="preload" href="/assets/js/2.bc9beebf.js" as="script"><link rel="preload" href="/assets/js/281.81b66752.js" as="script"><link rel="prefetch" href="/assets/js/10.829fbe09.js"><link rel="prefetch" href="/assets/js/100.21819a92.js"><link rel="prefetch" href="/assets/js/101.cba9a2fb.js"><link rel="prefetch" href="/assets/js/102.866a5b15.js"><link rel="prefetch" href="/assets/js/103.fdb7598a.js"><link rel="prefetch" href="/assets/js/104.75b80350.js"><link rel="prefetch" href="/assets/js/105.3030b5b8.js"><link rel="prefetch" href="/assets/js/106.11a5a8f2.js"><link rel="prefetch" href="/assets/js/107.232bdd92.js"><link rel="prefetch" href="/assets/js/108.a619d3d9.js"><link rel="prefetch" href="/assets/js/109.d31480d9.js"><link rel="prefetch" href="/assets/js/11.18a3d2d0.js"><link rel="prefetch" href="/assets/js/110.7b7051ad.js"><link rel="prefetch" href="/assets/js/111.6976153b.js"><link rel="prefetch" href="/assets/js/112.935a63bd.js"><link rel="prefetch" href="/assets/js/113.77296509.js"><link rel="prefetch" href="/assets/js/114.3ab3be4a.js"><link rel="prefetch" href="/assets/js/115.9b0dac10.js"><link rel="prefetch" href="/assets/js/116.55bc2cd6.js"><link rel="prefetch" href="/assets/js/117.75bd6d05.js"><link rel="prefetch" href="/assets/js/118.0dae142d.js"><link rel="prefetch" href="/assets/js/119.b1a2888c.js"><link rel="prefetch" href="/assets/js/12.81e292a4.js"><link rel="prefetch" href="/assets/js/120.6a3fd99f.js"><link rel="prefetch" href="/assets/js/121.ea1e1714.js"><link rel="prefetch" href="/assets/js/122.125ebe03.js"><link rel="prefetch" href="/assets/js/123.ec5bcef7.js"><link rel="prefetch" href="/assets/js/124.f346a2dc.js"><link rel="prefetch" href="/assets/js/125.11061d5a.js"><link rel="prefetch" href="/assets/js/126.7f8bc26b.js"><link rel="prefetch" href="/assets/js/127.26df99b6.js"><link rel="prefetch" href="/assets/js/128.0d4aae4f.js"><link rel="prefetch" href="/assets/js/129.ec01b50e.js"><link rel="prefetch" href="/assets/js/13.89c37359.js"><link rel="prefetch" href="/assets/js/130.9e570618.js"><link rel="prefetch" href="/assets/js/131.ffaed293.js"><link rel="prefetch" href="/assets/js/132.6ef77a2c.js"><link rel="prefetch" href="/assets/js/133.893078b6.js"><link rel="prefetch" href="/assets/js/134.a5229ebf.js"><link rel="prefetch" href="/assets/js/135.e1015261.js"><link rel="prefetch" href="/assets/js/136.655ac222.js"><link rel="prefetch" href="/assets/js/137.4c18a7dc.js"><link rel="prefetch" href="/assets/js/138.17cf92aa.js"><link rel="prefetch" href="/assets/js/139.7e23a82f.js"><link rel="prefetch" href="/assets/js/14.5a11397f.js"><link rel="prefetch" href="/assets/js/140.570f33f7.js"><link rel="prefetch" href="/assets/js/141.eb538cc8.js"><link rel="prefetch" href="/assets/js/142.2f38d42f.js"><link rel="prefetch" href="/assets/js/143.a32de735.js"><link rel="prefetch" href="/assets/js/144.76a63ee6.js"><link rel="prefetch" href="/assets/js/145.42483d13.js"><link rel="prefetch" href="/assets/js/146.068119e8.js"><link rel="prefetch" href="/assets/js/147.64de6f65.js"><link rel="prefetch" href="/assets/js/148.5709a561.js"><link rel="prefetch" href="/assets/js/149.9373f1d3.js"><link rel="prefetch" href="/assets/js/15.f98080cd.js"><link rel="prefetch" href="/assets/js/150.85328596.js"><link rel="prefetch" href="/assets/js/151.8179d926.js"><link rel="prefetch" href="/assets/js/152.357ef7ac.js"><link rel="prefetch" href="/assets/js/153.a7f37700.js"><link rel="prefetch" href="/assets/js/154.259e0731.js"><link rel="prefetch" href="/assets/js/155.609478f3.js"><link rel="prefetch" href="/assets/js/156.c02413b4.js"><link rel="prefetch" href="/assets/js/157.4225246d.js"><link rel="prefetch" href="/assets/js/158.90aa3193.js"><link rel="prefetch" href="/assets/js/159.48a36d7f.js"><link rel="prefetch" href="/assets/js/16.c2c39a12.js"><link rel="prefetch" href="/assets/js/160.1483c525.js"><link rel="prefetch" href="/assets/js/161.935da955.js"><link rel="prefetch" href="/assets/js/162.623fec51.js"><link rel="prefetch" href="/assets/js/163.9b588340.js"><link rel="prefetch" href="/assets/js/164.30ec5aba.js"><link rel="prefetch" href="/assets/js/165.189a8317.js"><link rel="prefetch" href="/assets/js/166.5af6c2ec.js"><link rel="prefetch" href="/assets/js/167.9dff1ac7.js"><link rel="prefetch" href="/assets/js/168.622f7001.js"><link rel="prefetch" href="/assets/js/169.2e7169b6.js"><link rel="prefetch" href="/assets/js/17.b69c805b.js"><link rel="prefetch" href="/assets/js/170.722cbe91.js"><link rel="prefetch" href="/assets/js/171.bc79c387.js"><link rel="prefetch" href="/assets/js/172.ee6817b8.js"><link rel="prefetch" href="/assets/js/173.b35d740e.js"><link rel="prefetch" href="/assets/js/174.5c6e7df5.js"><link rel="prefetch" href="/assets/js/175.d4a5903b.js"><link rel="prefetch" href="/assets/js/176.7be8b3ab.js"><link rel="prefetch" href="/assets/js/177.21a314f9.js"><link rel="prefetch" href="/assets/js/178.7c287ca3.js"><link rel="prefetch" href="/assets/js/179.b4682242.js"><link rel="prefetch" href="/assets/js/18.8b504010.js"><link rel="prefetch" href="/assets/js/180.0098b166.js"><link rel="prefetch" href="/assets/js/181.1ade00c6.js"><link rel="prefetch" href="/assets/js/182.2cdebd59.js"><link rel="prefetch" href="/assets/js/183.019718a7.js"><link rel="prefetch" href="/assets/js/184.d4ab9703.js"><link rel="prefetch" href="/assets/js/185.50d60e28.js"><link rel="prefetch" href="/assets/js/186.3a98e144.js"><link rel="prefetch" href="/assets/js/187.9ed4524b.js"><link rel="prefetch" href="/assets/js/188.c3a8a353.js"><link rel="prefetch" href="/assets/js/189.b949cdaa.js"><link rel="prefetch" href="/assets/js/19.6860ea74.js"><link rel="prefetch" href="/assets/js/190.7a54fb5e.js"><link rel="prefetch" href="/assets/js/191.804d06a5.js"><link rel="prefetch" href="/assets/js/192.c729ba9b.js"><link rel="prefetch" href="/assets/js/193.e8d492ad.js"><link rel="prefetch" href="/assets/js/194.49b4668e.js"><link rel="prefetch" href="/assets/js/195.e4508c24.js"><link rel="prefetch" href="/assets/js/196.538420c6.js"><link rel="prefetch" href="/assets/js/197.56fda656.js"><link rel="prefetch" href="/assets/js/198.75e420ad.js"><link rel="prefetch" href="/assets/js/199.67784aef.js"><link rel="prefetch" href="/assets/js/20.82f2e898.js"><link rel="prefetch" href="/assets/js/200.b32cd750.js"><link rel="prefetch" href="/assets/js/201.594db75b.js"><link rel="prefetch" href="/assets/js/202.70b380ec.js"><link rel="prefetch" href="/assets/js/203.0de30338.js"><link rel="prefetch" href="/assets/js/204.360c28c7.js"><link rel="prefetch" href="/assets/js/205.5b4b553b.js"><link rel="prefetch" href="/assets/js/206.6bd13fc8.js"><link rel="prefetch" href="/assets/js/207.875be01b.js"><link rel="prefetch" href="/assets/js/208.00694e2b.js"><link rel="prefetch" href="/assets/js/209.1fa15603.js"><link rel="prefetch" href="/assets/js/21.3f050c1d.js"><link rel="prefetch" href="/assets/js/210.f11e4fe3.js"><link rel="prefetch" href="/assets/js/211.a9beca8a.js"><link rel="prefetch" href="/assets/js/212.2fb3a3b1.js"><link rel="prefetch" href="/assets/js/213.c2fb7dcf.js"><link rel="prefetch" href="/assets/js/214.7613f2f7.js"><link rel="prefetch" href="/assets/js/215.ac00cd4b.js"><link rel="prefetch" href="/assets/js/216.926eb535.js"><link rel="prefetch" href="/assets/js/217.e03af99a.js"><link rel="prefetch" href="/assets/js/218.0212a1a9.js"><link rel="prefetch" href="/assets/js/219.318a1817.js"><link rel="prefetch" href="/assets/js/22.f2a18e55.js"><link rel="prefetch" href="/assets/js/220.34f368ec.js"><link rel="prefetch" href="/assets/js/221.1d325330.js"><link rel="prefetch" href="/assets/js/222.f8ac9e1e.js"><link rel="prefetch" href="/assets/js/223.1d081b10.js"><link rel="prefetch" href="/assets/js/224.d4a3c833.js"><link rel="prefetch" href="/assets/js/225.86046773.js"><link rel="prefetch" href="/assets/js/226.ee01e5ed.js"><link rel="prefetch" href="/assets/js/227.cc700349.js"><link rel="prefetch" href="/assets/js/228.121d2708.js"><link rel="prefetch" href="/assets/js/229.411a1a59.js"><link rel="prefetch" href="/assets/js/23.8416a606.js"><link rel="prefetch" href="/assets/js/230.ce1cd57d.js"><link rel="prefetch" href="/assets/js/231.9e415c00.js"><link rel="prefetch" href="/assets/js/232.af1434b0.js"><link rel="prefetch" href="/assets/js/233.aedf0a69.js"><link rel="prefetch" href="/assets/js/234.7343179f.js"><link rel="prefetch" href="/assets/js/235.3ea94188.js"><link rel="prefetch" href="/assets/js/236.e632719a.js"><link rel="prefetch" href="/assets/js/237.34a70a59.js"><link rel="prefetch" href="/assets/js/238.9582b4be.js"><link rel="prefetch" href="/assets/js/239.2a1f2f8d.js"><link rel="prefetch" href="/assets/js/24.73629fb4.js"><link rel="prefetch" href="/assets/js/240.e0563eb9.js"><link rel="prefetch" href="/assets/js/241.7a2b4d6e.js"><link rel="prefetch" href="/assets/js/242.f9f59053.js"><link rel="prefetch" href="/assets/js/243.95904034.js"><link rel="prefetch" href="/assets/js/244.0c91ecaa.js"><link rel="prefetch" href="/assets/js/245.58988353.js"><link rel="prefetch" href="/assets/js/246.73360878.js"><link rel="prefetch" href="/assets/js/247.3f36cb13.js"><link rel="prefetch" href="/assets/js/248.9bca99ac.js"><link rel="prefetch" href="/assets/js/249.e78a3dbb.js"><link rel="prefetch" href="/assets/js/25.f62ba06b.js"><link rel="prefetch" href="/assets/js/250.5999a7b3.js"><link rel="prefetch" href="/assets/js/251.5204362e.js"><link rel="prefetch" href="/assets/js/252.a731f0e2.js"><link rel="prefetch" href="/assets/js/253.ef15afff.js"><link rel="prefetch" href="/assets/js/254.8e0d8aa8.js"><link rel="prefetch" href="/assets/js/255.53408731.js"><link rel="prefetch" href="/assets/js/256.3d08ae8f.js"><link rel="prefetch" href="/assets/js/257.1de6306c.js"><link rel="prefetch" href="/assets/js/258.b3ec2e6c.js"><link rel="prefetch" href="/assets/js/259.40e6c088.js"><link rel="prefetch" href="/assets/js/26.ed3d7d52.js"><link rel="prefetch" href="/assets/js/260.05ea317d.js"><link rel="prefetch" href="/assets/js/261.048af235.js"><link rel="prefetch" href="/assets/js/262.905ce2e2.js"><link rel="prefetch" href="/assets/js/263.8ace396b.js"><link rel="prefetch" href="/assets/js/264.10841fe2.js"><link rel="prefetch" href="/assets/js/265.f94f5548.js"><link rel="prefetch" href="/assets/js/266.bf7b6e06.js"><link rel="prefetch" href="/assets/js/267.4c03cbf4.js"><link rel="prefetch" href="/assets/js/268.1886e607.js"><link rel="prefetch" href="/assets/js/269.9dfca98b.js"><link rel="prefetch" href="/assets/js/27.18a2fe1c.js"><link rel="prefetch" href="/assets/js/270.a0ebdd62.js"><link rel="prefetch" href="/assets/js/271.cb9ce68b.js"><link rel="prefetch" href="/assets/js/272.99ea7c05.js"><link rel="prefetch" href="/assets/js/273.7f5c9d16.js"><link rel="prefetch" href="/assets/js/274.1ca50474.js"><link rel="prefetch" href="/assets/js/275.e0a79a93.js"><link rel="prefetch" href="/assets/js/276.5e9f441d.js"><link rel="prefetch" href="/assets/js/277.31f82a4b.js"><link rel="prefetch" href="/assets/js/278.2df5557a.js"><link rel="prefetch" href="/assets/js/279.6caef400.js"><link rel="prefetch" href="/assets/js/28.30687ae2.js"><link rel="prefetch" href="/assets/js/280.07da039a.js"><link rel="prefetch" href="/assets/js/282.a362880b.js"><link rel="prefetch" href="/assets/js/283.37de8db2.js"><link rel="prefetch" href="/assets/js/284.31a9bcd5.js"><link rel="prefetch" href="/assets/js/285.e1ed9d70.js"><link rel="prefetch" href="/assets/js/286.5cd73851.js"><link rel="prefetch" href="/assets/js/287.b598b99a.js"><link rel="prefetch" href="/assets/js/288.a2580005.js"><link rel="prefetch" href="/assets/js/289.83413eb3.js"><link rel="prefetch" href="/assets/js/29.8f48fd0e.js"><link rel="prefetch" href="/assets/js/290.405974a0.js"><link rel="prefetch" href="/assets/js/291.f762f6f9.js"><link rel="prefetch" href="/assets/js/292.f5851afc.js"><link rel="prefetch" href="/assets/js/293.8e7d9ffa.js"><link rel="prefetch" href="/assets/js/294.3694f780.js"><link rel="prefetch" href="/assets/js/295.84d9cd4c.js"><link rel="prefetch" href="/assets/js/296.0a6bbe5d.js"><link rel="prefetch" href="/assets/js/297.195f8ede.js"><link rel="prefetch" href="/assets/js/3.7494bd83.js"><link rel="prefetch" href="/assets/js/30.5f000d59.js"><link rel="prefetch" href="/assets/js/31.2f3aea00.js"><link rel="prefetch" href="/assets/js/32.78912726.js"><link rel="prefetch" href="/assets/js/33.4f600444.js"><link rel="prefetch" href="/assets/js/34.1ec8259c.js"><link rel="prefetch" href="/assets/js/35.bc414639.js"><link rel="prefetch" href="/assets/js/36.b0c05f62.js"><link rel="prefetch" href="/assets/js/37.7e626d70.js"><link rel="prefetch" href="/assets/js/38.bb3a6a22.js"><link rel="prefetch" href="/assets/js/39.3d17059f.js"><link rel="prefetch" href="/assets/js/4.ff6020d1.js"><link rel="prefetch" href="/assets/js/40.2023aaeb.js"><link rel="prefetch" href="/assets/js/41.64452f22.js"><link rel="prefetch" href="/assets/js/42.32522c9c.js"><link rel="prefetch" href="/assets/js/43.ae24bdff.js"><link rel="prefetch" href="/assets/js/44.3acc6c82.js"><link rel="prefetch" href="/assets/js/45.2cec263d.js"><link rel="prefetch" href="/assets/js/46.ceacfcd8.js"><link rel="prefetch" href="/assets/js/47.59030139.js"><link rel="prefetch" href="/assets/js/48.fa5e3c59.js"><link rel="prefetch" href="/assets/js/49.2054398b.js"><link rel="prefetch" href="/assets/js/5.f2ade644.js"><link rel="prefetch" href="/assets/js/50.47a76469.js"><link rel="prefetch" href="/assets/js/51.c36ce30b.js"><link rel="prefetch" href="/assets/js/52.72ce6650.js"><link rel="prefetch" href="/assets/js/53.9dbe65dd.js"><link rel="prefetch" href="/assets/js/54.b571c195.js"><link rel="prefetch" href="/assets/js/55.3502ffd5.js"><link rel="prefetch" href="/assets/js/56.790119c4.js"><link rel="prefetch" href="/assets/js/57.aae4081b.js"><link rel="prefetch" href="/assets/js/58.e54d1920.js"><link rel="prefetch" href="/assets/js/59.6eaa8c48.js"><link rel="prefetch" href="/assets/js/6.04d544f0.js"><link rel="prefetch" href="/assets/js/60.7b2d3e47.js"><link rel="prefetch" href="/assets/js/61.093542b8.js"><link rel="prefetch" href="/assets/js/62.d0af83fd.js"><link rel="prefetch" href="/assets/js/63.b57f13fb.js"><link rel="prefetch" href="/assets/js/64.acbb2295.js"><link rel="prefetch" href="/assets/js/65.87d05864.js"><link rel="prefetch" href="/assets/js/66.773a0404.js"><link rel="prefetch" href="/assets/js/67.c66984d0.js"><link rel="prefetch" href="/assets/js/68.77bfcb54.js"><link rel="prefetch" href="/assets/js/69.7e3d1cc4.js"><link rel="prefetch" href="/assets/js/7.bc46b052.js"><link rel="prefetch" href="/assets/js/70.dbd5eb9b.js"><link rel="prefetch" href="/assets/js/71.109923c3.js"><link rel="prefetch" href="/assets/js/72.c92d7f25.js"><link rel="prefetch" href="/assets/js/73.a8ef2055.js"><link rel="prefetch" href="/assets/js/74.79119a72.js"><link rel="prefetch" href="/assets/js/75.009bcb5e.js"><link rel="prefetch" href="/assets/js/76.cba94ffd.js"><link rel="prefetch" href="/assets/js/77.caa07e47.js"><link rel="prefetch" href="/assets/js/78.fef64e9e.js"><link rel="prefetch" href="/assets/js/79.72d6eb81.js"><link rel="prefetch" href="/assets/js/8.8cf6541b.js"><link rel="prefetch" href="/assets/js/80.9370a1bd.js"><link rel="prefetch" href="/assets/js/81.f53b6c0a.js"><link rel="prefetch" href="/assets/js/82.01861305.js"><link rel="prefetch" href="/assets/js/83.4d69c6c5.js"><link rel="prefetch" href="/assets/js/84.ea80ae96.js"><link rel="prefetch" href="/assets/js/85.34c10294.js"><link rel="prefetch" href="/assets/js/86.4c7d6d58.js"><link rel="prefetch" href="/assets/js/87.ade6c1ee.js"><link rel="prefetch" href="/assets/js/88.306bb690.js"><link rel="prefetch" href="/assets/js/89.ed02954c.js"><link rel="prefetch" href="/assets/js/9.b3d99320.js"><link rel="prefetch" href="/assets/js/90.8db20502.js"><link rel="prefetch" href="/assets/js/91.3d01b6a4.js"><link rel="prefetch" href="/assets/js/92.b3dcea87.js"><link rel="prefetch" href="/assets/js/93.28790aa5.js"><link rel="prefetch" href="/assets/js/94.45722d7d.js"><link rel="prefetch" href="/assets/js/95.c04b6585.js"><link rel="prefetch" href="/assets/js/96.7c4d9328.js"><link rel="prefetch" href="/assets/js/97.85bb6089.js"><link rel="prefetch" href="/assets/js/98.e22a9c08.js"><link rel="prefetch" href="/assets/js/99.fd9f1f1d.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c17b3ac5.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo/TB-mini.png" alt="Teng's blog" class="logo"> <span class="site-name can-hide">Teng's blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/note/java/" class="nav-link">Java</a></div><div class="nav-item"><a href="/note/font-end/" class="nav-link">H5前端</a></div> <a href="https://github.com/shetengteng" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/note/java/" class="nav-link">Java</a></div><div class="nav-item"><a href="/note/font-end/" class="nav-link">H5前端</a></div> <a href="https://github.com/shetengteng" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/pages/c29ae4/" class="sidebar-link">背景介绍</a></li><li><a href="/pages/b38488/" class="sidebar-link">安装</a></li><li><a href="/pages/388924/" class="sidebar-link">配置说明</a></li><li><a href="/pages/ff1ac4/" class="sidebar-link">基础命令</a></li><li><a href="/pages/2c8a0a/" aria-current="page" class="active sidebar-link">常用模块</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#command" class="sidebar-link">command</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#查看说明" class="sidebar-link">查看说明</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#示例" class="sidebar-link">示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#shell" class="sidebar-link">shell</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#script" class="sidebar-link">script</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#copy" class="sidebar-link">copy</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#fetch" class="sidebar-link">fetch</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#file" class="sidebar-link">file</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#hostname" class="sidebar-link">hostname</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#cron" class="sidebar-link">cron</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#yum" class="sidebar-link">yum</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#service" class="sidebar-link">service</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#user" class="sidebar-link">user</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#group" class="sidebar-link">group</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#unarchive-解包解压缩" class="sidebar-link">unarchive 解包解压缩</a></li><li class="sidebar-sub-header"><a href="/pages/2c8a0a/#archive-打包压缩" class="sidebar-link">archive 打包压缩</a></li></ul></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Playbook</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-0c557b5e><div class="articleInfo" data-v-0c557b5e><ul class="breadcrumbs" data-v-0c557b5e><li data-v-0c557b5e><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-0c557b5e></a></li> <li data-v-0c557b5e><a href="/note/ansible/#Tool-Ansible" data-v-0c557b5e>Tool-Ansible</a></li></ul> <div class="info" data-v-0c557b5e><div title="作者" class="author iconfont icon-touxiang" data-v-0c557b5e><a href="https://github.com/shetengteng" target="_blank" title="作者" class="beLink" data-v-0c557b5e>Shetengteng</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-0c557b5e><a href="javascript:;" data-v-0c557b5e>2022-05-13</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><!---->常用模块<!----></h1>  <div class="theme-vdoing-content content__default"><p>模块文档 https://docs.ansible.com/ansible/latest/modules/modules_by_category.html</p> <h2 id="command"><a href="#command" class="header-anchor">#</a> command</h2> <blockquote><p>在远程主机执行命令，默认模块，可忽略-m选项</p></blockquote> <h3 id="查看说明"><a href="#查看说明" class="header-anchor">#</a> 查看说明</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible-doc command</span>
<span class="token operator">&gt;</span> COMMAND    <span class="token punctuation">(</span>/usr/lib/python2.7/site-packages/ansible/modules/commands/command.py<span class="token punctuation">)</span>
<span class="token punctuation">..</span>.
OPTIONS <span class="token punctuation">(</span><span class="token operator">=</span> is mandatory<span class="token punctuation">)</span>:
<span class="token punctuation">..</span>.
- chdir
        Change into this directory before running the command.
        <span class="token punctuation">[</span>Default: <span class="token punctuation">(</span>null<span class="token punctuation">)</span><span class="token punctuation">]</span>
        type: path
        version_added: <span class="token number">0.6</span>
<span class="token punctuation">..</span>.
- creates
        A filename or <span class="token punctuation">(</span>since <span class="token number">2.0</span><span class="token punctuation">)</span> glob pattern. If it already exists, this step
        *won't* be run.
        <span class="token punctuation">[</span>Default: <span class="token punctuation">(</span>null<span class="token punctuation">)</span><span class="token punctuation">]</span>
        type: path
<span class="token punctuation">..</span>.
- removes
        A filename or <span class="token punctuation">(</span>since <span class="token number">2.0</span><span class="token punctuation">)</span> glob pattern. If it already exists, this step
        *will* be run.
        <span class="token punctuation">[</span>Default: <span class="token punctuation">(</span>null<span class="token punctuation">)</span><span class="token punctuation">]</span>
        type: path
        version_added: <span class="token number">0.8</span>
<span class="token punctuation">..</span>.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p>**注意：**此命令不支持 <code>$VARNAME</code>  变量名  <code>&lt;</code> <code>&gt;</code> 重定向 <code>|</code> 管道符 <code>;</code> <code>&amp;</code> 等操作符，需要用shell模块实现</p> <p>常用参数说明</p> <ul><li>chdir:   进入到被管理主机目录</li> <li>creates: 如果有一个目录是存在，将不会运行命令</li> <li>removes: 如果有一个目录存在，将会执行命令</li> <li>默认存不存在目录，都执行</li></ul> <p>参数使用</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -a <span class="token string">'chdir=/data/ ls'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="示例"><a href="#示例" class="header-anchor">#</a> 示例</h3> <p><code>-a</code>  表示输入的参数，args的简写</p> <p>示例1</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible srvs -m <span class="token builtin class-name">command</span> -a <span class="token string">'service vsftpd start'</span>
ansible srvs -m <span class="token builtin class-name">command</span> -a <span class="token string">'echo adong |passwd --stdin 123456'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>示例2
创建并列出文件夹，推荐使用file模块创建文件夹</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -a 'mkdir /opt/my-ansible-test'</span>
<span class="token punctuation">[</span>WARNING<span class="token punctuation">]</span>: Consider using the <span class="token function">file</span> module with <span class="token assign-left variable">state</span><span class="token operator">=</span>directory rather than running <span class="token string">'mkdir'</span><span class="token builtin class-name">.</span>  If you need
to use <span class="token builtin class-name">command</span> because <span class="token function">file</span> is insufficient you can <span class="token function">add</span> <span class="token string">'warn: false'</span> to this <span class="token builtin class-name">command</span> task or <span class="token builtin class-name">set</span>
<span class="token string">'command_warnings=False'</span> <span class="token keyword">in</span> ansible.cfg to get rid of this message.
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>

<span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># [root@linux101 ~]# ansible all -a 'removes=/opt/my-ansible-test ls -dl /opt/my-ansible-test'</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -a 'removes=/opt/my-ansible-test2 ls -dl /opt/my-ansible-test'</span>
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>目录存在则执行</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -a 'removes=/opt/my-ansible-test ls -dl /opt/my-ansible-test'</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
drwxr-xr-x. <span class="token number">2</span> root root <span class="token number">4096</span> <span class="token number">5</span>月  <span class="token number">14</span> <span class="token number">23</span>:19 /opt/my-ansible-test
<span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -a 'removes=/opt/my-ansible-test2 ls -dl /opt/my-ansible-test'</span>
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> SUCCESS <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
skipped, since /opt/my-ansible-test2 does not exist
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>示例3
查看分区</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -a 'df -h'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="shell"><a href="#shell" class="header-anchor">#</a> shell</h2> <blockquote><p>和command相似，用shell执行命令</p></blockquote> <p>使用shell可以执行command模块中不能使用的特殊符号</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -m shell -a 'echo $HOSTNAME'</span>
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
linux103
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
linux102
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
linux104
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>调用bash执行命令 类似 cat /tmp/stanley.md | awk -F'|' '{print $1,$2}' &amp;&gt; /tmp/example.txt  这些复杂命令，即使使用shell也可能会失败，解决办法：写到脚本时，copy到远程执行，再把需要的结果拉回执行命令的机器</p> <p>可以修改ansible的配置文件，使shell作为默认模块</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">vim</span> /etc/ansible/ansible.cfg
module_name <span class="token operator">=</span> shell
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>示例1</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m shell  -a <span class="token string">'getenforce'</span>  查看SELINUX状态
ansible all -m shell  -a <span class="token string">&quot;sed -i 's/SELINUX=.*/SELINUX=disabled' /etc/selinux/config&quot;</span>
ansible srv -m shell -a <span class="token string">'echo magedu |passwd –stdin wang'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>示例2</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible 192.168.10.102 -m shell -a 'ls /opt'</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
apache-zookeeper-3.5.9-bin.tar.gz
jdk-8u212-linux-x64.tar.gz
kafka_2.12-3.0.0.tgz
module
my-ansible-test
rh

<span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible 192.168.10.102 -m shell -a 'rm -rf  /opt/my-ansible-test'</span>
<span class="token punctuation">[</span>WARNING<span class="token punctuation">]</span>: Consider using the <span class="token function">file</span> module with <span class="token assign-left variable">state</span><span class="token operator">=</span>absent rather than running <span class="token string">'rm'</span><span class="token builtin class-name">.</span>  If you need to use
<span class="token builtin class-name">command</span> because <span class="token function">file</span> is insufficient you can <span class="token function">add</span> <span class="token string">'warn: false'</span> to this <span class="token builtin class-name">command</span> task or <span class="token builtin class-name">set</span>
<span class="token string">'command_warnings=False'</span> <span class="token keyword">in</span> ansible.cfg to get rid of this message.
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">|</span> <span class="token assign-left variable">rc</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&gt;&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="script"><a href="#script" class="header-anchor">#</a> script</h2> <blockquote><p>在远程主机上运行ansible服务器上的脚本</p></blockquote> <p>使用 ansible-doc script查看使用帮助文档，可参考帮助文档的EXAMPLE</p> <p>常用参数</p> <ul><li><code>--some-arguments</code> 给sh脚本传递参数</li></ul> <p>示例
创建执行脚本</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># vim ansible-test.sh</span>

<span class="token comment">#!/bin/bash</span>
<span class="token function">hostname</span>
<span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># chmod +x ansible-test.sh</span>
<span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># sh ansible-test.sh </span>
linux101
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>使用script执行</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -m script -a '/opt/ansible-test.sh'</span>
<span class="token number">192.168</span>.10.103 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;rc&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;stderr&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;Shared connection to 192.168.10.103 closed.<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stderr_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;Shared connection to 192.168.10.103 closed.&quot;</span>
    <span class="token punctuation">]</span>, 
    <span class="token string">&quot;stdout&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;linux103<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stdout_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;linux103&quot;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;rc&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;stderr&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;Shared connection to 192.168.10.102 closed.<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stderr_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;Shared connection to 192.168.10.102 closed.&quot;</span>
    <span class="token punctuation">]</span>, 
    <span class="token string">&quot;stdout&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;linux102<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stdout_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;linux102&quot;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;rc&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;stderr&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;Shared connection to 192.168.10.104 closed.<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stderr_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;Shared connection to 192.168.10.104 closed.&quot;</span>
    <span class="token punctuation">]</span>, 
    <span class="token string">&quot;stdout&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;linux104<span class="token entity" title="\r">\r</span><span class="token entity" title="\n">\n</span>&quot;</span>, 
    <span class="token string">&quot;stdout_lines&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">[</span>
        <span class="token string">&quot;linux104&quot;</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><h2 id="copy"><a href="#copy" class="header-anchor">#</a> copy</h2> <blockquote><p>从主控端复制文件到远程主机</p></blockquote> <p>常用参数</p> <ul><li><p>src</p> <ul><li>源文件</li> <li>指定拷贝文件的本地路径</li> <li>如果以 / 结尾，则拷贝整个路径下的所有文件</li></ul></li> <li><p>dest</p> <ul><li>指定目标路径</li></ul></li> <li><p>mode</p> <ul><li>设置权限</li></ul></li> <li><p>backup</p> <ul><li>备份源文件，如果文件已经存在，进行备份操作</li></ul></li> <li><p>content</p> <ul><li>代替src</li> <li>指定本机文件内容,生成目标主机文件</li></ul></li></ul> <p>示例：先创建一个txt文件，然后拷贝到其他主机上</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># ansible all -m copy -a 'src=/opt/helloansible.txt dest=/opt/ backup=yes'</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;ansible_facts&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;discovered_interpreter_python&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/usr/bin/python&quot;</span>
    <span class="token punctuation">}</span>, 
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;checksum&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;8689129d4dd7f38370086412eaca315ae9a45611&quot;</span>, 
    <span class="token string">&quot;dest&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/opt/helloansible.txt&quot;</span>, 
    <span class="token string">&quot;gid&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;group&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;root&quot;</span>, 
    <span class="token string">&quot;md5sum&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;486df049b1cbc6eb9724459369698794&quot;</span>, 
    <span class="token string">&quot;mode&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;0644&quot;</span>, 
    <span class="token string">&quot;owner&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;root&quot;</span>, 
    <span class="token string">&quot;secontext&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;system_u:object_r:usr_t:s0&quot;</span>, 
    <span class="token string">&quot;size&quot;</span><span class="token builtin class-name">:</span> <span class="token number">14</span>, 
    <span class="token string">&quot;src&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/root/.ansible/tmp/ansible-tmp-1652579931.17-21255-124200668103281/source&quot;</span>, 
    <span class="token string">&quot;state&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;file&quot;</span>, 
    <span class="token string">&quot;uid&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span>
<span class="token punctuation">..</span>.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>示例：如果目标存在，默认覆盖，此处指定先备份，同时可以设置mode和owner</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m copy -a <span class="token string">&quot;src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：指定内容，直接生成目标文件</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m copy -a <span class="token string">&quot;content='test content<span class="token entity" title="\n">\n</span>xxx' dest=/tmp/test.txt&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="fetch"><a href="#fetch" class="header-anchor">#</a> fetch</h2> <blockquote><p>从远程主机提取文件至主控端，copy相反，目前不支持目录,可以先打包,再提取文件
如抓取日志操作</p></blockquote> <p>查看说明 ansible-doc -s fetch</p> <p>常用参数</p> <ul><li>dest
<ul><li>将抓取的文件放在指定的目录下</li> <li>如：设置dest为 /backup，远端服务192.168.10.101机器上的/opt/hello.txt文件被抓取，则最终目录是/backup/192.169.10.101/opt/hello.txt</li></ul></li> <li>src
<ul><li>只能是文件，不能是目录</li> <li>只能抓取单个文件</li></ul></li></ul> <p>示例</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># ansible all -m fetch -a 'src=/opt/helloansible.txt dest=/opt/ansible-fetch'</span>
<span class="token number">192.168</span>.10.104 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;checksum&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;8689129d4dd7f38370086412eaca315ae9a45611&quot;</span>, 
    <span class="token string">&quot;dest&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/opt/ansible-fetch/192.168.10.104/opt/helloansible.txt&quot;</span>, 
    <span class="token string">&quot;md5sum&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;486df049b1cbc6eb9724459369698794&quot;</span>, 
    <span class="token string">&quot;remote_checksum&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;8689129d4dd7f38370086412eaca315ae9a45611&quot;</span>, 
    <span class="token string">&quot;remote_md5sum&quot;</span><span class="token builtin class-name">:</span> null
<span class="token punctuation">}</span>
<span class="token punctuation">..</span>.
<span class="token comment"># 会生成每个被管理主机不同编号的目录,不会发生文件名冲突</span>
<span class="token punctuation">[</span>root@linux101 opt<span class="token punctuation">]</span><span class="token comment"># tree /opt/ansible-fetch/</span>
/opt/ansible-fetch/
├── <span class="token number">192.168</span>.10.102
│   └── opt
│       └── helloansible.txt
├── <span class="token number">192.168</span>.10.103
│   └── opt
│       └── helloansible.txt
└── <span class="token number">192.168</span>.10.104
    └── opt
        └── helloansible.txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>示例：如果远程的文件很多，但是fetch只能抓取一个文件，可以先打包</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m shell -a <span class="token string">'tar jxvf test.tar.gz /root/test.sh'</span>
ansible all -m fetch -a <span class="token string">'src=/root/test.tar.gz dest=/data/'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="file"><a href="#file" class="header-anchor">#</a> file</h2> <blockquote><p>设置文件属性</p></blockquote> <p>常用参数</p> <ul><li>path
<ul><li>要管理的文件路径</li> <li>是dest 和 name的别名</li> <li>强制添加</li></ul></li> <li>recurse
<ul><li>递归</li> <li>文件夹要用递归</li></ul></li> <li>src
<ul><li>指定源目标</li> <li>创建硬链接，软链接，需要配合'state=link' 'state=hard'</li></ul></li> <li>state
<ul><li>状态</li> <li>link，hard，touch（创建文件），directory（创建文件夹），absent（删除操作）</li></ul></li></ul> <p>示例：创建文件</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible 192.168.10.102 -m file -a 'path=/opt/file3.txt state=touch'</span>
<span class="token number">192.168</span>.10.102 <span class="token operator">|</span> CHANGED <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;ansible_facts&quot;</span><span class="token builtin class-name">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;discovered_interpreter_python&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/usr/bin/python&quot;</span>
    <span class="token punctuation">}</span>, 
    <span class="token string">&quot;changed&quot;</span><span class="token builtin class-name">:</span> true, 
    <span class="token string">&quot;dest&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;/opt/file3.txt&quot;</span>, 
    <span class="token string">&quot;gid&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;group&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;root&quot;</span>, 
    <span class="token string">&quot;mode&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;0644&quot;</span>, 
    <span class="token string">&quot;owner&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;root&quot;</span>, 
    <span class="token string">&quot;secontext&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;unconfined_u:object_r:usr_t:s0&quot;</span>, 
    <span class="token string">&quot;size&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>, 
    <span class="token string">&quot;state&quot;</span><span class="token builtin class-name">:</span> <span class="token string">&quot;file&quot;</span>, 
    <span class="token string">&quot;uid&quot;</span><span class="token builtin class-name">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>示例：删除文件
注意：在删除文件夹的时候，如果使用 /xx/* ，那么删除不了xx下的文件夹以及文件，直接使用/xx/即可</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -m file -a 'path=/opt/file2.txt state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：创建文件夹</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token punctuation">[</span>root@linux101 ~<span class="token punctuation">]</span><span class="token comment"># ansible all -m file -a 'path=/opt/file state=directory'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：设置权限</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m <span class="token function">file</span> -a <span class="token string">&quot;path=/root/test.sh owner=wang mode=755&quot;</span>  设置权限755
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：创建软链接</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m <span class="token function">file</span> -a <span class="token string">'src=/data/testfile dest=/data/testfile-link state=link'</span> 创建软链接
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="hostname"><a href="#hostname" class="header-anchor">#</a> hostname</h2> <blockquote><p>管理主机名</p></blockquote> <p>示例：更改一个group的主机名</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible appsrvs -m <span class="token function">hostname</span> -a <span class="token string">&quot;name=app.stt.com&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：更改单个主机名</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible <span class="token number">192.168</span>.38.103 -m <span class="token function">hostname</span> -a <span class="token string">&quot;name=app2.stt.com&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="cron"><a href="#cron" class="header-anchor">#</a> cron</h2> <blockquote><p>计划任务
支持时间：minute,hour,day,month,weekday</p></blockquote> <p>示例：创建任务</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m <span class="token function">cron</span> -a <span class="token string">'minute=*/5 job=&quot;&quot;/usr/sbin/ntpdate 172.16.0.1 &amp;&gt;/dev/null&quot; name=Synctime'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：删除任务</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m <span class="token function">cron</span> -a <span class="token string">'state=absent name=Synctime'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：注释任务，不再生效，必须添加name；如果启用则设置disabled=no，或者disabled=false</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m <span class="token function">cron</span> -a <span class="token string">'minute=*/10 job=&quot;/usr/sbin/ntpdate 172.30.0.100&quot; name=synctime disabled=yes'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>注意：在命令中的job如果包含多个参数，含有空格，那么job=后面需要用双引号括起来</p> <h2 id="yum"><a href="#yum" class="header-anchor">#</a> yum</h2> <p>安装的前提是其他机器都安装了yum以配置了相同的数据源</p> <p>示例：查看程序列表</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token comment"># 依据名称查询</span>
ansible websrvs -m yum -a <span class="token string">'list=httpd'</span>
<span class="token comment"># 依据状态查询</span>
ansible all -m yum -a <span class="token string">'list=installed'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>示例：安装，state默认present</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m yum -a <span class="token string">'name=httpd state=present'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：卸载，可以使用state=absent 或 state=removed</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m yum -a <span class="token string">'name=httpd state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：安装多个包</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m yum -a <span class="token string">'name=vsftpd,memcached,httpd'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：卸载多个</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m yum -a <span class="token string">'name=vsftpd,memcached,httpd state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：安装本地的rpm包</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m yum -a <span class="token string">'name=/root/vsftpd-3.0.2-22.e17.x86_64.rpm'</span>
<span class="token comment"># 禁用gpg check</span>
ansible all -m yum -a <span class="token string">'name=/root/vsftpd-3.0.2-22.e17.x86_64.rpm disable_gpg_check=yes'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>示例：更新某个软件的yum缓存，再安装</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m yum -a <span class="token string">'name=dstat update_cache=yes'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="service"><a href="#service" class="header-anchor">#</a> service</h2> <blockquote><p>服务管理</p></blockquote> <p>常用参数</p> <ul><li>arguments
<ul><li>可选参数</li></ul></li> <li>enabled
<ul><li>设置是否开机启动</li></ul></li> <li>name
<ul><li>服务名称</li></ul></li> <li>state
<ul><li>stopped，reloaded，restarted，started</li></ul></li></ul> <p>示例：停止服务</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m <span class="token function">service</span> -a <span class="token string">'name=httpd state=stopped'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：启动服务，并设置为开机启动</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m <span class="token function">service</span> -a <span class="token string">'name=httpd state=started enabled=yes'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：重新加载</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m <span class="token function">service</span> -a <span class="token string">'name=httpd state=reloaded'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：重启服务</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m <span class="token function">service</span> -a <span class="token string">'name=httpd state=restarted'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="user"><a href="#user" class="header-anchor">#</a> user</h2> <blockquote><p>管理用户</p></blockquote> <p>常用参数</p> <ul><li>home
<ul><li>指定家目录路径</li></ul></li> <li>system
<ul><li>指定系统账号</li></ul></li> <li>group
<ul><li>指定组</li></ul></li> <li>groups
<ul><li>指定辅助组</li></ul></li> <li>remove
<ul><li>清除账户，删除夹目录</li></ul></li> <li>shell
<ul><li>指定shell类型</li></ul></li> <li>comment
<ul><li>描述</li></ul></li> <li>state
<ul><li>与上诉其他命令一致</li> <li>默认present，创建</li></ul></li></ul> <p>示例：增加用户user1，主组是root，home目录是/app/user1，指定uid</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m user -a <span class="token string">'name=user1 comment=&quot;test user&quot; uid=2048 home=/app/user1 group=root'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：增加系统用户 system=yes</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m user -a <span class="token string">'name=sysuser1 system=yes home=/app/sysuser1'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：清空用户所有数据，包含家目录文件夹数据</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m user -a <span class="token string">'name=user1 state=absent remove=yes'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：创建用户</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m user -a <span class="token string">'name=app uid=88 system=yes home=/app groups=root,bin shell=/sbin/nologin password=&quot;$1$zfVojmPy$ZILcvxnXljvTI2PhP2Iqv1&quot;'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：删除账户，但不删除家目录</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m user -a <span class="token string">'name=app state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="group"><a href="#group" class="header-anchor">#</a> group</h2> <blockquote><p>用户组管理</p></blockquote> <p>常用参数</p> <ul><li>name</li> <li>state，默认present</li> <li>system，是否是系统组</li> <li>gid，组id</li></ul> <p>示例：创建组</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m group -a <span class="token string">'name=testgroup system=yes gid=80'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：删除组</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m group -a <span class="token string">'name=testgroup state=absent'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="unarchive-解包解压缩"><a href="#unarchive-解包解压缩" class="header-anchor">#</a> unarchive 解包解压缩</h2> <blockquote><p>解包解压缩</p></blockquote> <p>使用方法</p> <ul><li>将ansible主机上的压缩包传到远程主机后解压缩至特定目录，设置copy=yes</li> <li>将远程主机上的某个压缩包解压缩到指定路径下，设置copy=no</li></ul> <p>常见参数</p> <ul><li><p>copy</p> <ul><li>默认yes</li> <li>当copy=yes时，拷贝的文件是从ansible主机复制到远程主机上</li> <li>当copy=no时，在远程主机上寻找src源文件</li></ul></li> <li><p>src</p> <ul><li>源路径</li> <li>是ansible主机上的路径，或者是远程主机上的路径</li> <li>如果是远程主机上的路径，则需要设置copy=no</li></ul></li> <li><p>dest</p> <ul><li>远程主机上的目标路径</li></ul></li> <li><p>mode</p> <ul><li>设置解压缩后的文件权限</li></ul></li></ul> <p>示例：默认copy为yes ,将本机目录文件解压到目标主机对应目录下</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m unarchive -a <span class="token string">'src=foo.tgz dest=/var/lib/foo'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：解压被管理主机的foo.zip到data目录下, 并设置权限777</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible websrvs -m unarchive -a <span class="token string">'src=/tmp/foo.zip dest=/data copy=no mode=0777'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例：从网络上下载并解压，注意copy=no</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code> ansible websrvs -m unarchive -a <span class="token string">'src=https://example.com/example.zip dest=/data copy=no'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="archive-打包压缩"><a href="#archive-打包压缩" class="header-anchor">#</a> archive 打包压缩</h2> <p>常用参数</p> <ul><li>path，指定路径</li> <li>dest，指定目标文件</li> <li>format，指定打包格式</li> <li>owner，指定所属者</li> <li>mode，设置权限</li></ul> <p>示例</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>ansible all -m archive -a <span class="token string">'path=/etc/sysconfig dest=/data/sysconfig.tar.bz2 format=bz2 owner=wang mode=0777'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></div></div>  <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">2022/05/22, 12:42:00</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/ff1ac4/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">基础命令</div></a> <a href="/pages/eb387a/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">简单介绍</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/ff1ac4/" class="prev">基础命令</a></span> <span class="next"><a href="/pages/eb387a/">简单介绍</a>→
      </span></p></div></div></div> <!----></main></div> <div class="footer"><div class="icons"><a href="mailto:work_stt@163.com" title="email" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/shetengteng" title="GitHub" target="_blank" class="iconfont icon-github"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2021-2022
    <span>Shetengteng | MIT License</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.ab6a7ec7.js" defer></script><script src="/assets/js/2.bc9beebf.js" defer></script><script src="/assets/js/281.81b66752.js" defer></script>
  </body>
</html>
